﻿Imports Mysoft.Map.Data
Imports Mysoft.Map.Application.Security.User
Public Class AuthHeader
    Inherits System.Web.Services.Protocols.SoapHeader

    Public UserCode As String                   '请求服务的用户名称
    Public UserPass As String                   '请求服务的用户密码 md5加密


    Public Sub New()

    End Sub

    Public Sub New(ByVal strCode As String, ByVal strPass As String)
        UserCode = strCode
        UserPass = strPass
    End Sub

    Public Function Authenticate() As Boolean
        Return Authenticate(String.Empty)
    End Function

    Public Function Authenticate(ByRef strRtn As String) As Boolean
        Return Authenticate(strRtn, False)
    End Function

    Public Function Authenticate(ByRef strRtn As String, ByVal excuteSQL As Boolean) As Boolean
        If UserCode.Length = 0 Then
            strRtn = "FAIL|登录用户代码为空！"
            Return False
        End If

        If checkUser(excuteSQL) = False Then
            strRtn = "FAIL|用户未通过身份验证！"
            Return False
        End If

        Return True
    End Function

    Private Function checkUser(ByVal excuteSQL As Boolean) As Boolean
        Dim mySession = HttpContext.Current.Session

        If Not mySession Is Nothing AndAlso mySession("UserCode") <> String.Empty Then
            Return True
        End If

        Try
            Dim sql As String
            Dim dt As DataTable
            sql = String.Format("SELECT userguid,username FROM myuser WHERE usercode='{0}' AND password='{1}'", UserCode, UserPass)

            dt = MyDB.GetDataTable(sql)

            If dt.Rows.Count > 0 Then
                mySession("UserCode") = UserCode
                mySession("UserName") = dt.Rows(0).Item("username").ToString
                mySession("UserGuid") = dt.Rows(0).Item("userguid").ToString
                If excuteSQL Then
                    Return CheckUserRight(mySession("UserGUID"), "00000120", "02")
                Else
                    Return True
                End If
            Else
                Return False
            End If
        Catch ex As Exception
            Return False
        End Try
    End Function
End Class
